package com.secsbrain.development.dal.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
import com.secsbrain.development.ribbon.DirectResolveRule;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

@Configuration
public class ConsumerRibbonConfiguration {

    @Bean
    @ConfigurationProperties("secsbrain.ribbon")
    public RibbonResolveMapConfig ribbonResolveMapConfig() {
        return new RibbonResolveMapConfig();
    }

    @Bean
    @ConditionalOnMissingBean
    public IRule defaultRule() {
        return new RoundRobinRule();
    }

    @Bean
    @Scope(SCOPE_PROTOTYPE)
    @Primary
    public IRule ribbonRule(RibbonResolveMapConfig ribbonResolveMapConfig, IRule iRule) {
        DirectResolveRule directResolveRule = new DirectResolveRule();
        directResolveRule.setDefaultRule(iRule);
        directResolveRule.setRibbonResolveMapConfig(ribbonResolveMapConfig);
        return directResolveRule;
    }

}
